// created by noellynch
// Jun 3, 2009

package com.sceneworker.jmetreeextensions;

import java.util.List;

import javax.swing.Icon;

import com.acarter.composabletree.ComposableTreeNode;
import com.acarter.jmejtree.JMEJTreeIcon;
import com.jme.image.Texture;
import com.jme.scene.Geometry;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.app.globals.cacheobjects.ISceneWorkerCacheObject;
import com.sceneworker.app.globals.cacheobjects.RenderToTextureCacheObject;

public class TextureTreeNodeSpatialTreeNode implements ComposableTreeNode {

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getChild(java.lang.Object,
	 *      int)
	 */
	public Object getChild(Object parent, int index) {
		if((parent != null) && (parent instanceof Texture)) {
			ISceneWorkerCacheObject		lcl_co = SceneWorkerGlobals.inst().getFromCache("rtt" + parent.hashCode());
			
			if(lcl_co == null) {
				return null;
			}
			
			if(lcl_co instanceof RenderToTextureCacheObject) {
				return ((RenderToTextureCacheObject)lcl_co).mcl_scenes;
			}
		}
		
		return null;
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getChildCount(java.lang.Object)
	 */
	public int getChildCount(Object parent) {
		if((parent != null) && (parent instanceof Texture)) {
			ISceneWorkerCacheObject		lcl_co = SceneWorkerGlobals.inst().getFromCache("rtt" + parent.hashCode());
			
			if(lcl_co == null) {
				return 0;
			}
			
			if(lcl_co instanceof RenderToTextureCacheObject) {
				return 1;
			}
		}
		
		return 0;
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getChildIndex(java.lang.Object,
	 *      java.lang.Object)
	 */
	public int getChildIndex(Object parent, Object child) {
		if((parent != null) && (parent instanceof Texture)) {
			ISceneWorkerCacheObject		lcl_co = SceneWorkerGlobals.inst().getFromCache("rtt" + parent.hashCode());
			
			if(lcl_co == null) {
				return 0;
			}
			
			if(lcl_co instanceof RenderToTextureCacheObject) {
				return 0;
			}
		}

		return -1;
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getParent(java.lang.Object)
	 */
	public Object getParent(Object child) {
		if(child instanceof Spatial)
			return ((Spatial) child).getParent();

		return null;
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getNodeClassType()
	 */
	public Class<?> getNodeClassType() {

		return Spatial.class;
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getIcon(Object)
	 */
	public Icon getIcon(Object node) {

		if(node instanceof Node)
			return JMEJTreeIcon.getIcon().node;
		else if(node instanceof Geometry)
			return JMEJTreeIcon.getIcon().geometry;

		return null;
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getNodeText(Object, boolean,
	 *      boolean, boolean, int, boolean)
	 */
	public String getNodeText(Object node, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

		if(node instanceof Spatial)
			return ((Spatial) node).getName();

		return (node != null) ? node.toString() : "";
	}

	/**
	 * @see com.acarter.composabletree.ComposableTreeNode#getNodeToolTipText(Object,
	 *      boolean, boolean, boolean, int, boolean)
	 */
	public String getNodeToolTipText(Object node, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

		return (node != null) ? node.toString() : "";
	}

}
